import { math, Vec2, Vec3 } from "cc";

export class Utils {
    static jumpTo_v3(to: Vec3, height: number): Readonly<Vec3> {
        return { value: to, progress: this._jumpTo_progress(3, height) } as any as Vec3;
    }
    static jumpTo_v2(to: Vec3, height: number): Readonly<Vec2> {
        return { value: to, progress: this._jumpTo_progress(2, height) } as any as Vec2;
    }
    private static _jumpTo_progress(max: number, height: number): (from: number, to: number, cur: number, pcs: number) => number {
        let i = max;
        let heightSqrt = Math.sqrt(height);
        return (from: number, to: number, cur: number, pcs: number) => {

            // 使用序列耦合区分xyz轴: 1: x, 2: y, 3: z
            if (i >= max) i = 1;
            else i++;

            // let rsl = from + (to - from) * pcs; // lerp
            let rsl = math.lerp(from, to, pcs);

            if (i === 2) { // y轴的增量算法
                let du = Math.abs(1 - pcs * 2); // [0,1] > [1,0,1]
                rsl += height - Math.pow(heightSqrt * du, 2);
            }

            return rsl;
        };
    }
}

